package com.clz.workorder.adapter

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.clz.workorder.R
import com.clz.workorder.databinding.ItemReportBinding
import com.clz.workorder.fragment.ReportListFragment
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.RepairListBean
import com.czl.base.util.DialogHelper
import com.czl.base.util.ToastHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/10 11:38
 */
class ReportAdapter(private val fragment: ReportListFragment) :
    BaseQuickAdapter<RepairListBean.Data, BaseDataBindingHolder<ItemReportBinding>>(
        R.layout.item_report
    ) {

    var currentView: View? = null

    override fun convert(
        holder: BaseDataBindingHolder<ItemReportBinding>,
        item: RepairListBean.Data
    ) {
        holder.dataBinding?.apply {
            data = item
            adapter = this@ReportAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is RepairListBean.Data) {
            dealOption("查看", it.no)
        }
    })

    val onRenewClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is RepairListBean.Data) {
            DialogHelper.showMoreDialog(
                context,
                currentView!!,
                when (it.status) {
                    "待接单", "处理中" -> arrayOf("查看", "撤销")
                    "待支付" -> arrayOf("查看", "支付")
                    "待评价" -> arrayOf("查看", "评价", "删除")
                    "已评价" -> arrayOf("查看", "删除")
                    "待审核" -> arrayOf("查看")
                    else -> arrayOf("")
                }
            ) { _, text ->
                dealOption(text, it.no)
            }

        }
    })

    val onCurrentDot: BindingCommand<View> = BindingCommand(BindingConsumer {
        currentView = it
    })

    private fun dealOption(text: String, no: String) {
        when (text) {
            "查看" -> {
                fragment.startContainerActivity(
                    AppConstants.Router.Order.F_ORDER_DETAIL,
                    Bundle().apply {
                        putString(AppConstants.BundleKey.WORK_ORDER_NO, no)
                        putBoolean(AppConstants.BundleKey.WORK_ORDER_IS_MINE,true)
                    })
            }
            "撤销" -> fragment.viewModel.uc.cancelOrderEvent.postValue(no)
            "支付" -> ToastHelper.showNormalToast(text)
            "评价" -> fragment.viewModel.uc.navScoreEvent.postValue(no)
            "删除" -> fragment.viewModel.uc.deleteOrderEvent.postValue(no)
        }
    }
}